<template>
  <div>

    <userHome></userHome>

    <div class="search-page">
      <!-- 搜索区域 -->
      <div class="search-area">
        <div class="search-box">
          <input
              v-model="keyword"
              type="text"
              placeholder="请输入搜索关键词"
              @keyup.enter="handleSearch"
          >
          <button @click="handleSearch" :disabled="!keyword.trim()">
            搜索
          </button>
        </div>
      </div>

      <!-- 结果区域 -->
      <div class="search-result-container">
        
        <!-- 加载状态 -->
        <div v-if="loading" class="loading">
          <div class="loading-spinner"></div>
          <span>正在搜索...</span>
        </div>

        <!-- 搜索结果列表 -->
        <div v-else>
          <div v-if="searchResults.length === 0" class="no-result">
            <el-empty description="未找到相关结果"></el-empty>
          </div>
          <div v-else class="result-list">

            <h2 v-if="searchResults" class="search-title">共找到
              <span style="color: #F64848">{{ total }}</span>
              条与<span style="color: #F64848">{{ keywordTemp }}</span>相关的帖子</h2>

            <div v-for="item in currentPageData"
                 :key="item.nid"
                 class="result-item" @click="router.push(`/post/${item.nid}`)">
              <h3 v-html="highlightText(item.title)"></h3>

              <div class="description-content">
                <p v-html="highlightText(item.describe)"></p>
              </div>
              <p >失踪人：<span v-html="highlightText(item.seekerName)"></span> | 性别：{{ item.sex==='male'?'男':'女' }} | 年龄：{{ item.age }}岁 | 身高：{{ item.height }}cm</p>
              <p >失踪日期：{{ new Date(item.missingDate).toLocaleDateString() }} | 失踪地点：<span v-html="highlightText(item.missingAddress)"></span></p>
            </div>

            <!-- 添加分页器 -->
            <div class="pagination-container">
              <el-pagination
                v-model:current-page="currentPage"
                v-model:page-size="pageSize"
                :page-sizes="[10, 20, 30, 50]"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                layout="total, sizes, prev, pager, next"
              />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import userHome from '@/views/home/userHome.vue'
import {searchNoteByKeyWordAPI} from "@/apis/notes";

const searchResults = ref([]); // 搜索结果列表
const loading = ref(false);     // 加载状态
const keyword = ref('');        // 搜索关键词
const keywordTemp = ref('');

const route = useRoute();
const router = useRouter();

//分页相关
const currentPage = ref(1)
const pageSize = ref(50)
const total = ref(0)

//计算当前页
const currentPageData = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value
  const end = start + pageSize.value
  return searchResults.value.slice(start, end)
})

//处理页码改变
const handleCurrentChange = (val) => {
  currentPage.value = val
}

//处理每页显示数量改变
const handleSizeChange = (val) => {
  pageSize.value = val
  currentPage.value = 1
}

// 高亮显示匹配文本
const highlightText = (text) => {
  if (!keywordTemp.value || !text) return text
  const reg = new RegExp(keywordTemp.value, 'gi');
  return text.replace(reg, match => `<span style="color: #F64848;font-weight: bold">${match}</span>`);
};

// 获取搜索结果
const fetchSearchResults = async () => {
  if (!keyword.value.trim()) {
    return
  }
  loading.value = true
  const res = await searchNoteByKeyWordAPI(keyword.value)
  if(res.code==='200'){
    searchResults.value = res.data
    total.value = res.data.length
    loading.value = false
    keywordTemp.value = keyword.value

    currentPage.value = 1 // 重置页码
  }
}

// 处理搜索
const handleSearch = () => {
  fetchSearchResults();
  // 更新路由参数
  router.push({
    query: { keyword: keyword.value }
  });
};

// 从路由获取搜索关键词
onMounted(() => {
  keyword.value = route.query.keyword || '';
  if (keyword.value) {
    fetchSearchResults();
  }
});
</script>

<style scoped>
.search-page {
  min-height: 100vh;
  background-color: #f5f7fa;
}

.search-area {
  background-color: #fff;
  padding: 20px 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.search-box {
  max-width: 800px;
  margin: 0 auto;
  display: flex;
  gap: 10px;
  padding: 0 20px;
}

.search-box input {
  flex: 1;
  padding: 12px 20px;
  border: 2px solid #e0e3e9;
  border-radius: 6px;
  font-size: 16px;
  transition: all 0.3s;
}

.search-box input:focus {
  outline: none;
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.1);
}

.search-box button {
  padding: 12px 30px;
  background-color: #409eff;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s;
}

.search-box button:hover {
  background-color: #66b1ff;
}

.search-box button:disabled {
  background-color: #a0cfff;
  cursor: not-allowed;
}

.search-result-container {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
}

.search-title {
  margin: 20px 0;
  color: #333;
  font-size: 20px;
}

.loading {
  text-align: center;
  padding: 40px;
  color: #666;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 15px;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid #f3f3f3;
  border-top: 3px solid #409eff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.no-result {
  text-align: center;
  padding: 40px;
  color: #666;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.result-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.result-item {
  padding: 20px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.3s;
}

.result-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);

}

.result-item h3 {
  margin: 0 0 10px 0;
  color: #333;
  font-size: 18px;
}

.result-item p {
  margin: 0;
  color: #666;
  font-size: 14px;
  line-height: 1.6;
}

.description-content {
  line-height: 1.6;
  color: #333;
  font-size: 14px;
  background-color: #f8f9fa;
  padding: 20px;
  border-radius: 4px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style>
